local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	=  1.645

use "$results_fold/`dataset'.dta", clear

preserve
drop if bs == 0 | bs == -1
drop *_F_* *_N2_* *_N3_*
collapse (sd) *
unab varl: *b2* *b3* 
foreach var of local varl {
    rename `var' se`var'
}
replace bs = 0
save "$working/temp/bstemp", replace
restore
preserve
drop *_F_* *_N2_* *_N3_*
keep if bs == -1
unab varl: *b2* *b3*  //*b4*
foreach var of local varl {
    rename `var' se`var'
}
save "$working/temp/bstemp2", replace
restore
keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}

keep  *_b2* *_b3*
drop *_OI*   

gen id = _n
ren (*_b2size1_y *_b2size1_yn *_b2size1_ycn) (y2size1* yn2size1* ycn2size1*)
ren (*_b2size2_y *_b2size2_yn *_b2size2_ycn) (y2size2* yn2size2* ycn2size2*)
ren (*_b3size1_y *_b3size1_yn *_b3size1_ycn) (y3size1* yn3size1* ycn3size1*)
ren (*_b3size2_y *_b3size2_yn *_b3size2_ycn) (y3size2* yn3size2* ycn3size2*)
ren (*_I*) (I*)
reshape long y2size1 yn2size1 ycn2size1 y2size2 yn2size2 ycn2size2 y3size1 yn3size1 ycn3size1 y3size2 yn3size2 ycn3size2 ///
			 Iy2size1 Iyn2size1 Iycn2size1 Iy2size2 Iyn2size2 Iycn2size2 Iy3size1 Iyn3size1 Iycn3size1 Iy3size2 Iyn3size2 Iycn3size2 ///
			 y2size1se yn2size1se ycn2size1se y2size2se yn2size2se ycn2size2se y3size1se yn3size1se ycn3size1se y3size2se yn3size2se ycn3size2se ///
			 Iy2size1se Iyn2size1se Iycn2size1se Iy2size2se Iyn2size2se Iycn2size2se Iy3size1se Iyn3size1se Iycn3size1se Iy3size2se Iyn3size2se Iycn3size2se ///
			 , i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows2size1_`var' = `var'2size1 - ($sdv * `var'2size1se)
	gen highs2size1_`var' = `var'2size1 + ($sdv * `var'2size1se)
	gen lows2size2_`var' = `var'2size2 - ($sdv * `var'2size2se)
	gen highs2size2_`var' = `var'2size2 + ($sdv * `var'2size2se)
	gen lows3size1_`var' = `var'3size1 - ($sdv * `var'3size1se)
	gen highs3size1_`var' = `var'3size1 + ($sdv * `var'3size1se)
	gen lows3size2_`var' = `var'3size2 - ($sdv * `var'3size2se)
	gen highs3size2_`var' = `var'3size2 + ($sdv * `var'3size2se)
}


		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		sencode variables, gen(vars) gsort(ordering)
		gen vars1 = vars+.3
		gen vars2 = vars+.15
forval i=1/2 {
		twoway	(scatter y2size`i' vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2size`i'_y highs2size`i'_y vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2size`i'_y highs2size`i'_y vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter y3size`i' vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3size`i'_y highs3size`i'_y vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3size`i'_y highs3size`i'_y vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iy2size`i' vars1, mcolor(red) msymbol(square)) ///
				(rcap lows2size`i'_Iy highs2size`i'_Iy vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2size`i'_Iy highs2size`i'_Iy vars1 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2" 7 "Input Access") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) ///
				title("`samplenametext'") subtitle("With Year Fixed Effects")
			
			graph export "$figures/`samplenamefile'_y_2O1I_size`i'.png", as(png) replace
				
		twoway	(scatter yn2size`i' vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2size`i'_yn highs2size`i'_yn vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2size`i'_yn highs2size`i'_yn vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter yn3size`i' vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3size`i'_yn highs3size`i'_yn vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3size`i'_yn highs3size`i'_yn vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter Iyn2size`i' vars1, mcolor(red) msymbol(square)) ///
				(rcap lows2size`i'_Iyn highs2size`i'_Iyn vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
				(rcap lows2size`i'_Iyn highs2size`i'_Iyn vars1 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2" 7 "Input Access") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) ///
				title("`samplenametext'") subtitle("With Industry and Year Fixed Effects")
			
			graph export "$figures/`samplenamefile'_yn_2O1I_size`i'.png", as(png) replace
			
		twoway	(scatter ycn2size`i' vars, mcolor(blue) msymbol(O)) ///
				(rcap lows2size`i'_ycn highs2size`i'_ycn vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows2size`i'_ycn highs2size`i'_ycn vars if id == 1, lwidth(thin) color(gs6) ) ///
				(scatter ycn3size`i' vars2, mcolor(blue) msymbol(diamond)) ///
				(rcap lows3size`i'_ycn highs3size`i'_ycn vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
				(rcap lows3size`i'_ycn highs3size`i'_ycn vars2 if id == 1, lwidth(thin) color(gs6) ) ///
				, xlabel(1(1)7, angle(35) valuelabel) ///
				ylabel(,grid) ///
				legend(order(1 "Output Competition 1" 4 "Output Competition 2") size(medlarge) rows(1) region(lcolor(white)) position(6) ring(1)) scheme(s1color) ///
				title("`samplenametext'") subtitle("With Industry-Year Fixed Effects")
			
			graph export "$figures/`samplenamefile'_ycn_2O1I_size`i'.png", as(png) replace
}

